package com.dazzle.dzcamera;

import android.content.Context;
import android.graphics.Rect;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.dazzle.cameralib.CameraRecordManager;
import com.dazzle.cameralib.utils.DebugUtils;

public class MainActivity extends AppCompatActivity {


    public static String TAG = "MainActivity";
    protected Button mStartPreviewBt;
    protected Button mStartRecordBt;
    protected Button mShowHidedBt;
    protected Button mUpdatePreviewSizedBt;
    private Context mContext;

    private CameraRecordManager cameraRecordManager;

    static boolean flag = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.activity_main);
        mContext = this;
        mStartPreviewBt = (Button) findViewById(R.id.start_preview);
        mStartRecordBt = (Button) findViewById(R.id.start_record);
        mShowHidedBt = (Button) findViewById(R.id.show_hide);
        mUpdatePreviewSizedBt = (Button) findViewById(R.id.update_preview_size);

        cameraRecordManager = new CameraRecordManager(mContext);

        mStartPreviewBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DebugUtils.LogE(TAG, "click");
                cameraRecordManager.enablePreview(!cameraRecordManager.isPreviewEnable());
            }
        });
        mStartRecordBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DebugUtils.LogE(TAG, "click");
                cameraRecordManager.enableRecord(!cameraRecordManager.isRecording());
            }
        });
        mShowHidedBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DebugUtils.LogE(TAG, "click");
                if (cameraRecordManager.isPreviewHide()) {
                    cameraRecordManager.showPreviewWindow();
                }else {
                    cameraRecordManager.hidePreviewWindow();
                }

            }
        });
        mUpdatePreviewSizedBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DebugUtils.LogE(TAG, "click");
                if (flag) {
                    flag = false;
                    Rect rect= new Rect(1920-1280, 1080-720, 1920, 1080);
                    cameraRecordManager.setPreviewWindowRect(rect);
                }else {
                    flag = true;
                    Rect rect= new Rect(1920-1280/2, 1080-720/2, 1920, 1080);
                    cameraRecordManager.setPreviewWindowRect(rect);
                }

            }
        });
    }


    @Override
    public void onResume() {
        super.onResume();
        cameraRecordManager.init();
    }

    @Override
    protected void onPause() {
        super.onPause();
        cameraRecordManager.release();
    }
}
